package com.rpay.web.controller.wallet;

import com.rpay.common.dto.sms.SmsCodeDTO;
import com.rpay.common.dto.userWallet.UserWalletDTO;
import com.rpay.common.dto.userWallet.UserWalletQueryDTO;
import com.rpay.common.enums.PcsResultCode;
import com.rpay.common.enums.SmsCodeEnum;
import com.rpay.common.enums.SmsTypeEnum;
import com.rpay.common.exception.SmsException;
import com.rpay.common.vo.PcsResult;
import com.rpay.service.sms.ISmsCodeService;
import com.rpay.web.interceptor.authentic.AccessControl;
import com.rpay.web.interceptor.authentic.AccessControlType;
import com.rpay.web.param.UpdateUserWallet;
import com.rpay.web.param.UserWallet;
import com.rpay.web.service.UserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rpay.service.config.ServiceThreadLocal;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/18
 * @Description:钱包管理
 */
@RestController
@RequestMapping(value = "api/wallet")
@AccessControl(type = AccessControlType.ACCESS_TOKEN)
public class UserWalletController {
    @Autowired
    private UserWalletService userWalletService;
    @Autowired
    private ISmsCodeService iSmsCodeService;
    @Value("${sms.code.invalidTime}")
    private long invalidTime;

    @GetMapping(value = "")
    public PcsResult list() {
        int userId = ServiceThreadLocal.getUserId();
        UserWalletQueryDTO userWalletQueryDTO = new UserWalletQueryDTO();
        userWalletQueryDTO.setUserId(userId);
        UserWalletQueryDTO data = userWalletService.list(userWalletQueryDTO);
        return PcsResult.build(data);
    }

    @PostMapping(value = "")
    public PcsResult add(@Validated @RequestBody UserWallet userWallet) {
        //        验证验证码和邮箱是否正确
        SmsCodeDTO smsCodeDTO = new SmsCodeDTO();
        smsCodeDTO.setVerifyType(SmsCodeEnum.WALLET.getCode());
        smsCodeDTO.setType(SmsTypeEnum.MAIL.getCode());
        smsCodeDTO.setReceiver(userWallet.getEmail());
        smsCodeDTO.setCode(userWallet.getCode());
        SmsCodeDTO smsCode = iSmsCodeService.getSmsCode(smsCodeDTO);
        if (smsCode == null) {
            throw new SmsException(PcsResultCode.SMS_CODE_FAIL);
        }
        long failureTime = System.currentTimeMillis() - smsCode.getCreateTime().getTime();
        if (failureTime > invalidTime * 60 * 1000) {
            throw new SmsException(PcsResultCode.SMS_CODE_EXPIRED);
        }
        UserWalletDTO userWalletDTO = new UserWalletDTO();
        Integer userId = ServiceThreadLocal.getUserId();
        userWalletDTO.setUserId(userId);
        userWalletDTO.setWalletAddr(userWallet.getWalletAddr());
        userWalletDTO.setWalletTag(userWallet.getWalletTag());
        userWalletDTO.setRemark(userWallet.getRemark());
        userWalletService.post(userWalletDTO);
        return new PcsResult();
    }

    @DeleteMapping(value = "/detail/{id}")
    public PcsResult delete(@PathVariable Integer id) {
        Integer userId = ServiceThreadLocal.getUserId();
        UserWalletDTO userWalletDTO = new UserWalletDTO();
        userWalletDTO.setId(id);
        userWalletDTO.setUserId(userId);
        userWalletService.delete(userWalletDTO);
        return new PcsResult();
    }

    @PatchMapping(value = "/detail/{id}")
    public PcsResult patch(@Validated @RequestBody UpdateUserWallet updateUserWallet, @PathVariable("id") Integer id) {
        Integer userId = ServiceThreadLocal.getUserId();
        UserWalletDTO userWalletDTO = new UserWalletDTO();
        userWalletDTO.setId(id);
        userWalletDTO.setUserId(userId);
        userWalletDTO.setWalletAddr(updateUserWallet.getWalletAddr());
        userWalletDTO.setWalletTag(updateUserWallet.getWalletTag());
        userWalletDTO.setRemark(updateUserWallet.getRemark());
        userWalletService.patch(userWalletDTO);
        return new PcsResult();
    }
}
